#pragma once
#include <vector>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <utility>
#include "sys_lock.hpp"
template <typename TYPE>
void *_thread_t(void *param)
{
    ((TYPE *)param)->_thread();
    return NULL;
}

enum class ThreadType
{
    MASTER,
    SLAVE,
    INDEPENDENT
};

enum class ThreadStartType
{
    instant_create,
    suspended_create
};

/**
 * @brief For series of threads
 */
class CThread
{
public:
    CThread(std::string thread_mame, ThreadType thread_type, ThreadStartType thrd_start_type);
    void start();
    void exit();
    static void lock();
    static void unlock();
    friend void test_thread();

    void _thread();
    virtual void init() = 0;
    virtual void task() = 0;
    virtual void clean_up() = 0;
    ~CThread() { exit(); }

    /*!
    *  the period time it took for a run
    */
    std::string getThreadName(){return _name; }
    static void cutAllThread();
    static void printAllThread();
    static bool suspendThread(std::string thrd_name);
    static bool resumeThread(std::string thrd_name);
    static CCriticalSection cri_sec;
private:
    static std::vector<CThread*> _thread_list;
    std::string _name;
    ThreadType _type;
    ThreadStartType  _thrd_start_type;
    volatile bool _sync_lock;
    volatile bool _running;
    DWORD _thrd;
    HANDLE _htd;

};